package com.jiutong.meipengdai.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.ui.activitys.LoginActivity;
import com.jiutong.meipengdai.utils.SpConfig;

public abstract class BaseActivity extends AppCompatActivity {

    protected Context mContext;
    public String TAG = this.getClass().getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(getLayoutResouceId());
        mContext = this;
        AppManager.getAppManager().addActivity(this);

        //激活沉浸式状态栏  第三方库ImmersionBar
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();

        initDate(savedInstanceState);
        initView();
        initEvent();
    }

    public abstract int getLayoutResouceId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    protected void initView(){}

    protected void initDate(Bundle savedInstanceState){}

    protected void initEvent(){}

    public void openActivity(Class<?> pClass){
        openActivity(pClass, null);
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }

        if(pClass.isAnnotationPresent(CheckLoginInterface.class)){

            if(TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SESSIONID_STRING))){
                intent = new Intent(this,LoginActivity.class);
            }
        }

        startActivity(intent);
    }

    public void toastShortShow(String str){
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    public void toastLongShow(String str){
        Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();
    }

    public boolean isLogin(){
        if(TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SESSIONID_STRING))){
            openActivity(LoginActivity.class);
            return false;
        }
        return true;
    }
}
